package com.chinaunicom.research.bigdatalabs.util;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Random;

/**
 *
 * Created by zhangxr103 on 2014/11/26.
 */
public class Tools {

    public static int[]fixValue( int[] input) {
//        int []input={0, 0, 0, 0, 0, 1597, 322, 515, 4728, 5807, 8257, 19450, 19893, 20918, 23043, 18355, 12758};
        Random random=new Random();
        input[7]+=1000;
        input[10]+=4000;
        input[11]-=4030;

        for(int i=0;i<input.length;i++){
            if(i>input.length/2){
                input[i]=input[i]-input[i]/10+190-random.nextInt(230);
            }else{
                input[i]=input[i]+input[i]/20+190-random.nextInt(230);
            }
        }

        return input;
    }

    public static int[]printValue( int[] input) {
        int []value={0, 0, 0, 0, 0, 1597, 322, 515, 4728, 5807, 8257, 19450, 19893, 20918, 23043, 18355, 12758};
        for(int i:input){
            System.out.print(i+"\t");
        }
        return input;
    }

    public static Pair<int[],int[]> LoadData(String path,int total,int sp){
        int [] edge=new int[total/sp-1];
        int [] cnt=new int[total/sp-1];

        for(int i=0;i<edge.length;i++){
            edge[i]=sp*(i+1);
        }

        try {
            BufferedReader reader=new BufferedReader(new FileReader(path));
            String line;
            while ((line=reader.readLine())!=null){
                double d=Double.parseDouble(line.split("\\|")[1]);
                for(int i=0;i<edge.length;i++){
                    if(d <=edge[i] ){
                        cnt[i]++;
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(path);
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println(Arrays.toString(name).replaceAll("\\[", "'").replaceAll("\\]", "'").replaceAll(",", "','"));
//        System.out.println(Arrays.toString(cnt));

        return new Pair<int[], int[]>(edge,cnt);
    }

    public static String[] rangeName(int[] edge){
        String [] name=new String[edge.length];
        for(int i=0;i<edge.length;i++){
            if(i==0){
                name[i]=0+"分~"+edge[i]+"分";
            }else{
                name[i]=edge[i-1]+"分~"+edge[i]+"分";
            }
        }
        return name;
    }

    public static String Array2String(Object []a,int start,int end){
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = start;; i++) {
            b.append(a[i]);
            if (i == iMax||i==end)
                return b.append(']').toString();
            b.append(", ");
        }
    }

    public static String Array2String(int []a,int start,int end){
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = start;; i++) {
            b.append(a[i]);
            if (i == iMax||i==end)
                return b.append(']').toString();
            b.append(", ");
        }
    }

    /**
     * @param s o
     * @return true if s is not null or empty
     */
    public static boolean notNullOrEmpty(String s) {
        return s != null && !s.isEmpty() && !s.equals("null");
    }

    /**
     * @param s o
     * @return meaning true or none meaning false
     * @see #notNullOrEmpty(String)
     */
    public static boolean notNullOrEmpty(Collection s) {
        return s != null && !s.isEmpty();
    }

    /**
     * @param s o
     * @return meaning true or none meaning false
     * @see #notNullOrEmpty(String)
     */
    public static boolean notNullOrEmpty(Object[] s) {
        return s != null && s.length != 0;
    }

    /**
     * @param s o
     * @return meaning true or none meaning false
     * @see #notNullOrEmpty(String)
     */
    public static boolean notNullOrEmpty(Map s) {
        return s != null && !s.isEmpty();
    }



    /**
     * get score & postion
     * @param value
     * @param phone
     * @return
     */
    public static Pair<Integer,Integer> getPos( int[] value, String phone) {
        ConnDB db=ConnDB.instance();
        Pair<Integer,Integer> pair=null;
        ResultSet rs=db.query("select credit_score from credit_data where mobilePhone = '"+phone+"'");

        try {
            if(rs.next()){
                int scroe=rs.getInt(1);
                for(int i=0;i<value.length;i++){
                    if(scroe<value[i]){
                        return new Pair<Integer, Integer>(scroe,i);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.close();
        }
        return null;
    }


    public static String getID(String phone){
        ConnDB db=ConnDB.instance();
        ResultSet rs=db.query("select userId  from credit_data where mobilePhone = '"+phone+"'");
        try {
            if(rs.next()){
                return rs.getString(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int[]getDesc(String userid){
        ConnDB db=ConnDB.instance();

        int [] arr=new int[6];
        try {
            ResultSet rs1=db.query("select rent_cost,cost_count  from pay_data where userId = '"+userid+"'");
            if(rs1.next()){
                arr[0]=rs1.getInt(1);
                arr[1]=rs1.getInt(2);
            }
            rs1.close();

            ResultSet rs2=db.query("select birthday,online_time  from user_data where userId = '"+userid+"'");
            if(rs2.next()){
                arr[2]=rs2.getInt(1);
                arr[3]=rs2.getInt(2);
            }
//            rs2.close();

            ResultSet rs3=db.query("select cost_time  from voice_data where userId = '"+userid+"'");
            if(rs3.next()){
                arr[4]=rs3.getInt(1);
            }
//            rs3.close();

//            //TODO test
//            ResultSet rs4=db.query("select count(*)  from re_net_demo where userId = '"+userid+"'");
//            if(rs4.next()){
//                arr[5]=rs4.getInt(1);
//            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.close();
        }
        return arr;
    }

    public static void main(String...args){
//        Pair<String[],int[]> p=LoadData(Tools.class.getResource("CB.txt_score.csv").getPath(),900,50);
//        System.out.println(Arrays.toString(p.getValue()));

        System.out.println(Tools.class.getResource("/CB.txt_score.csv").getPath());
        Pair<int[], int[]> pair = Tools.LoadData(Tools.class.getResource("/CB.txt_score.csv").getPath(), 900, 50);

        System.out.println(Arrays.toString(pair.getValue()));


//        Pair<Integer,Integer> rs=getPos(pair.getKey(), "18601104443");
//        System.out.println(rs.getKey()+"\t"+rs.getValue());
//
//        String value_x = Tools.Array2String(Tools.rangeName(pair.getKey()), 0, pair.getKey().length);
////        String value_y = Tools.Array2String(pair.getValue(), 5, pair.getValue().length);
//
//        System.out.println(pair.getKey()[rs.getValue()]);
//        System.out.println(Arrays.toString(pair.getKey()));
//        System.out.println(value_x);
//
//        System.out.println(getID("18601104443"));
//        System.out.println(Arrays.toString(getDesc(getID("18601104443"))));


    }
}
